import * as api from '@/api/api';
import * as request from '@/api/request'
import Vue from 'vue'
import utils from '@/utils'
import config from '@/config'
const ServerMixin = {
    Event: new Vue()
};
// console.log(api);

ServerMixin.install = function(v, options) {
    v.prototype.$api = api.api;
    v.prototype.$request = request;
    v.prototype.$config = config;

    v.prototype.$$message = (options) => {
        let opts = {
            showClass: true
        };
        for (let i in options) {
            if (!utils.isEmpty(options[i])) {
                opts[i] = options[i];
            }
        }
        return v.prototype.$message(opts);
    }

    v.prototype.$$notify = (options) => {
        let opts = {};
        for (let i in options) {
            if (!utils.isEmpty(options[i])) {
                opts[i] = options[i];
            }
        }
        return v.prototype.$notify(opts);
    }

    v.prototype.$$confirm = (options) => {
        let opts = {
            title: '提示',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        };
        for (let i in options) {
            if (!utils.isEmpty(options[i])) {
                opts[i] = options[i];
            }
        }
        return v.prototype.$confirm(opts.message, opts.title, opts);
    }

    v.prototype.$$alert = (options) => {
        let opts = {
            title: '提示',
            confirmButtonText: '确定'
        };
        for (let i in options) {
            if (!utils.isEmpty(options[i])) {
                opts[i] = options[i];
            }
        }
        return v.prototype.$alert(opts.message, opts.title, opts);
    }
    v.prototype.$$tip = (options) => {
        utils.tip(options)
    }

    v.prototype.$updateArray = (arr, fieldName, fieldValue, newRow) => {
        //先查找是否有同样的主键（key），如果有，说明是修改。这里主键暂时不支持复合主键
        for (let i in arr) {
            if (arr[i][fieldName] == fieldValue) {
                for (let j in arr[i])
                    v.prototype.$set(arr[i], j, newRow[j]);
                break;
            }
        }
    };

}
export default ServerMixin;